par(mfrow=c(1,2))

###############################################
# GRAPHS FOR GROUND PROFILES WITH FIXED VALUES:
###############################################
stap=0.01;seq=seq(.005,.995,.01)
D1=dnorm(seq,0.40,0.04);D1=D1/(sum(D1)*stap)
D2=dnorm(seq,0.70,0.10);D2=D2/(sum(D2)*stap)
G1=(D1+D2)/2
G2=dnorm(seq,0.45,0.15);G2=G2/(sum(G2)*stap)
plot(seq,G1,col='blue',type='l',lwd=2,ylim=c(0,8),axes=FALSE,
main=c(paste('Estimated Respondent Profiles for'),
paste('Group 1 (mean=.55) and Group 2 (mean=.45)')),
xlab='Probability of Behaviour',ylab='Likelihood of Probability')
axis(side=1,at=seq(0,1,0.10));axis(side=2,at=seq(0,8,2),las=1)
lines(seq,G2,col='red',lwd=2)
legend('topright',bty='n',cex=.8,
c('Estimated Profile Group 1','Estimated Profile Group 2'),
fill=c('blue','red'))


# First for low number of respondents (50 per group)
# Second for high number of respondents (500 per group)


###############################################
# SAMPLE FOR SPECIFIED NUMBER OF MEN AND WOMEN:
###############################################
avesamp1=matrix(NA,100,10000)
avesamp2=matrix(NA,100,10000)
PBCD=numeric()
N=0
while(N<10000)
{

A=sample(c(0,1),100,replace=TRUE,prob=c(.5,.5));table(A)
n_S1=A[A==0];length(n_S1)
n_S2=A[A==1];length(n_S2)



#############################################################################
# SAMPLE RESPONDENT DATA OBSERVATIONS FROM GROUND PROFILES WITH FIXED VALUES:
#############################################################################
S1=sample(seq,length(n_S1),replace=TRUE,prob=G1);length(S1)
S2=sample(seq,length(n_S2),replace=TRUE,prob=G2);length(S2)



###################################################################
# SAMPLE SPECIFIED NUMBER OF ITEMS PER RESPONDENT DATA OBSERVATION:
###################################################################
items=500
SAMP1=matrix(0,length(n_S1),items)
for(x in 1:length(n_S1))
{
s=sample(c(0,1),items,replace=TRUE,prob=c(1-S1[x],S1[x]))
SAMP1[x,]=s
}

items=500
SAMP2=matrix(0,length(n_S2),items)
for(x in 1:length(n_S2))
{
s=sample(c(0,1),items,replace=TRUE,prob=c(1-S2[x],S2[x]))
SAMP2[x,]=s
}



######################################################
# CONSTRUCT ESTIMATED PROFILES BASED ON ITEM SAMPLING:
######################################################
stap=0.01;ncel=100;uit_samp1=matrix(0,ncel,1);n_samp1=0
for (i in 1:length(n_S1))
{
  db=dbinom(sum(SAMP1[i,]),sum(table(SAMP1[i,])),seq)
  uiti=matrix(db,ncel,1)/(sum(db)*stap)
  uit_samp1=uit_samp1+uiti
  n_samp1=n_samp1+1
}
uit_samp1=uit_samp1/n_samp1

stap=0.01;ncel=100;uit_samp2=matrix(0,ncel,1);n_samp2=0
for (i in 1:length(n_S2))
{
  db=dbinom(sum(SAMP2[i,]),sum(table(SAMP2[i,])),seq)
  uiti=matrix(db,ncel,1)/(sum(db)*stap)
  uit_samp2=uit_samp2+uiti
  n_samp2=n_samp2+1
}
uit_samp2=uit_samp2/n_samp2



avesamp1[,N+1]=uit_samp1
avesamp2[,N+1]=uit_samp2



###############################################
# ADAPTED CLIFF'S DELTA FOR ESTIMATED PROFILES:
###############################################
y1=uit_samp1;y2=uit_samp2
DIF=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
dif=(x-1)*stap-(y-1)*stap
DIF[x,y]=dif
}}
DIF=sign(DIF)
DEN=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
den=y1[x]*y2[y]*stap^2
DEN[x,y]=den
}}
CD_real=sum(DIF*DEN)/sum(DEN)


PBCD=c(PBCD,CD_real)
N=N+1;if(N%%1000==0)print(N)
}



##########################################
# CONSTRUCT AND PLOT CONFIDENCE INTERVALS:
##########################################
seq=seq(0.005,0.995,0.01)

X1=apply(avesamp1,1,mean)
Y1=apply(avesamp1,1,sd)
lines(seq,X1,col='magenta',lwd=2,lty=1)
K1=X1+2.576*Y1;lines(seq,K1,col='magenta',lty=2)
L1=X1-2.576*Y1;L1[L1<0]=0;lines(seq,L1,col='magenta',lty=2)

X2=apply(avesamp2,1,mean)
Y2=apply(avesamp2,1,sd)
lines(seq,X2,col='green3',lwd=2,lty=1)
K2=X2+2.576*Y2;lines(seq,K2,col='green3',lty=2)
L2=X2-2.576*Y2;L2[L2<0]=0;lines(seq,L2,col='green3',lty=2)





#######################################################





###############################################
# GRAPHS FOR GROUND PROFILES WITH FIXED VALUES:
###############################################
stap=0.01;seq=seq(.005,.995,.01)
D1=dnorm(seq,0.40,0.04);D1=D1/(sum(D1)*stap)
D2=dnorm(seq,0.70,0.10);D2=D2/(sum(D2)*stap)
G1=(D1+D2)/2
G2=dnorm(seq,0.45,0.15);G2=G2/(sum(G2)*stap)
plot(seq,G1,col='blue',type='l',lwd=2,ylim=c(0,8),axes=FALSE,
main=c(paste('Estimated Respondent Profiles for'),
paste('Group 1 (mean=.55) and Group 2 (mean=.45)')),
xlab='Probability of Behaviour',ylab='Likelihood of Probability')
axis(side=1,at=seq(0,1,0.10));axis(side=2,at=seq(0,8,2),las=1)
lines(seq,G2,col='red',lwd=2)
legend('topright',bty='n',cex=.8,
c('Estimated Profile Group 1','Estimated Profile Group 2'),
fill=c('blue','red'))


# First for low number of respondents (50 per group)
# Second for high number of respondents (500 per group)


###############################################
# SAMPLE FOR SPECIFIED NUMBER OF MEN AND WOMEN:
###############################################
avesamp1=matrix(NA,100,10000)
avesamp2=matrix(NA,100,10000)
PBCD=numeric()
N=0
while(N<10000)
{

A=sample(c(0,1),1000,replace=TRUE,prob=c(.5,.5));table(A)
n_S1=A[A==0];length(n_S1)
n_S2=A[A==1];length(n_S2)



#############################################################################
# SAMPLE RESPONDENT DATA OBSERVATIONS FROM GROUND PROFILES WITH FIXED VALUES:
#############################################################################
S1=sample(seq,length(n_S1),replace=TRUE,prob=G1);length(S1)
S2=sample(seq,length(n_S2),replace=TRUE,prob=G2);length(S2)



###################################################################
# SAMPLE SPECIFIED NUMBER OF ITEMS PER RESPONDENT DATA OBSERVATION:
###################################################################
items=500
SAMP1=matrix(0,length(n_S1),items)
for(x in 1:length(n_S1))
{
s=sample(c(0,1),items,replace=TRUE,prob=c(1-S1[x],S1[x]))
SAMP1[x,]=s
}

items=500
SAMP2=matrix(0,length(n_S2),items)
for(x in 1:length(n_S2))
{
s=sample(c(0,1),items,replace=TRUE,prob=c(1-S2[x],S2[x]))
SAMP2[x,]=s
}



######################################################
# CONSTRUCT ESTIMATED PROFILES BASED ON ITEM SAMPLING:
######################################################
stap=0.01;ncel=100;uit_samp1=matrix(0,ncel,1);n_samp1=0
for (i in 1:length(n_S1))
{
  db=dbinom(sum(SAMP1[i,]),sum(table(SAMP1[i,])),seq)
  uiti=matrix(db,ncel,1)/(sum(db)*stap)
  uit_samp1=uit_samp1+uiti
  n_samp1=n_samp1+1
}
uit_samp1=uit_samp1/n_samp1

stap=0.01;ncel=100;uit_samp2=matrix(0,ncel,1);n_samp2=0
for (i in 1:length(n_S2))
{
  db=dbinom(sum(SAMP2[i,]),sum(table(SAMP2[i,])),seq)
  uiti=matrix(db,ncel,1)/(sum(db)*stap)
  uit_samp2=uit_samp2+uiti
  n_samp2=n_samp2+1
}
uit_samp2=uit_samp2/n_samp2



avesamp1[,N+1]=uit_samp1
avesamp2[,N+1]=uit_samp2



###############################################
# ADAPTED CLIFF'S DELTA FOR ESTIMATED PROFILES:
###############################################
y1=uit_samp1;y2=uit_samp2
DIF=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
dif=(x-1)*stap-(y-1)*stap
DIF[x,y]=dif
}}
DIF=sign(DIF)
DEN=matrix(0,length(y1),length(y2))
for(x in 1:length(y1))
{
for(y in 1:length(y2))
{
den=y1[x]*y2[y]*stap^2
DEN[x,y]=den
}}
CD_real=sum(DIF*DEN)/sum(DEN)


PBCD=c(PBCD,CD_real)
N=N+1;if(N%%1000==0)print(N)
}



##########################################
# CONSTRUCT AND PLOT CONFIDENCE INTERVALS:
##########################################
seq=seq(0.005,0.995,0.01)

X1=apply(avesamp1,1,mean)
Y1=apply(avesamp1,1,sd)
lines(seq,X1,col='magenta',lwd=2,lty=1)
K1=X1+2.576*Y1;lines(seq,K1,col='magenta',lty=2)
L1=X1-2.576*Y1;L1[L1<0]=0;lines(seq,L1,col='magenta',lty=2)

X2=apply(avesamp2,1,mean)
Y2=apply(avesamp2,1,sd)
lines(seq,X2,col='green3',lwd=2,lty=1)
K2=X2+2.576*Y2;lines(seq,K2,col='green3',lty=2)
L2=X2-2.576*Y2;L2[L2<0]=0;lines(seq,L2,col='green3',lty=2)



